<?php
class qqloginControl extends skymvc{
	
	public function __construct(){
		parent::__construct();		
	}
	
	public function onInit(){
		require_once(ROOT_PATH."config/qq_config.php");
	}
	
public function onGeturl()
{
	$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
    $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
        . APPID . "&redirect_uri=" . urlencode(CALLBACK)
        . "&state=" . $_SESSION['state']
        . "&scope=".SCOPE;
	header("Location:$login_url");
	exit();
}

public function oncallback()
{
	require("api/qq/qqapi.php");		
	//QQ登录成功后的回调地址,主要保存access token
	qq_callback();	
	//获取用户标示id
	get_openid();	
	//echo $_SESSION['openid'];
	$arr=get_user_info();
	
	$nickname=$arr['nickname'];
	if(empty($nickname))
	{
		$this->goall('qq接口错误',1,0,'/index.php?m=index');
	}
	if($user=M("user")->getRow("SELECT *  FROM ".table('user')." WHERE openid='".$_SESSION['openid']."' AND xfrom='qq' "))
	{
		M('login')->set("ssuser",$user);
		//$_SESSION['ssuser']=$user;
		$this->goall('登陆成功',0,0,'/index.php');
	}else
	{
		//生成账户
		$i=0;
		$u=$nickname;
		while(M("user")->getOne("SELECT userid FROM ".table('user')." WHERE  nickname='$u' "))
		{
			$i++;
			$u=$nickname.$i;
		}
		
		//关联插件
		$data=array(
			"nickname"=>$u,
			"xfrom"=>'qq',
			"openid"=>$_SESSION['openid'],
			"dateline"=>time(),
			"lastfeed"=>time(),
		);
		if(isset($_COOKIE['invite_uid'])){
			$data['invite_userid']=intval($_COOKIE['invite_uid']);
		}
		$userid=M("user")->insert($data);
		$this->loadControl("inviteapi");
		$this->inviteapiControl->invite_reg($userid);
		$user=M("user")->getRow("SELECT * FROM ".table('user')." WHERE userid='$userid' ");
		M('login')->set("ssuser",$user);
		//账号绑定处理
		//$this->gourl("/index.php?m=register&a=OpenReg");
		$this->goall('注册登陆成功',0,0,'/index.php');
	}
	
	
	
}

	
	public function onApp(){
		$access_token=get_post('access_token','h');
		$openid=get_post('openid','h');
		$appid=get_post('appid','h');
	 		
		/**/
		$get_user_info = "https://graph.qq.com/user/get_user_info?"
        . "access_token=" . $access_token
        . "&oauth_consumer_key=" . $appid
        . "&openid=" . $openid
		. "&client_id=".$appid."" 
        . "&format=json";

		$info = curl_get_contents($get_user_info);
		file_put_contents("log.txt",$info);
		$arr = json_decode($info, true);
		/**/
		$nickname=$arr['nickname'];
		if(empty($nickname))
		{
			$this->goall('qq接口错误',1,0,'/index.php?m=index');
		}
		if($user=M("user")->getRow("SELECT *  FROM ".table('user')." WHERE openid='".$openid."' AND xfrom='qq' "))
		{
			M('login')->set("ssuser",$user);
			//$_SESSION['ssuser']=$user;
			$authcode=jiami($user['userid']."|".umd5($user['password']));
			setcookie("authcode",$authcode,time()+3600000,"/",DOMAIN);
			$redata=array(
				"authcode"=>$authcode
			);
			$this->goall('登陆成功',0,$redata,'/index.php');
		}else
		{
			//生成账户
			$i=0;
			$u=$nickname;
			while(M("user")->getOne("SELECT userid FROM ".table('user')." WHERE  nickname='$u' "))
			{
				$i++;
				$u=$nickname.$i;
			}
			
			//关联插件
			$data=array(
				"nickname"=>$u,
				"xfrom"=>'qq',
				"openid"=>$openid,
				"dateline"=>time(),
				"lastfeed"=>time(),
			);
			if(isset($_COOKIE['invite_uid'])){
				$data['invite_userid']=intval($_COOKIE['invite_uid']);
			}
			$userid=M("user")->insert($data);
			$this->loadControl("inviteapi");
			$this->inviteapiControl->invite_reg($userid);
			$user=M("user")->getRow("SELECT * FROM ".table('user')." WHERE userid='$userid' ");
			$authcode=jiami($user['userid']."|".umd5($user['password']));
			setcookie("authcode",$authcode,time()+3600000,"/",DOMAIN);
			M('login')->set("ssuser",$user);
			//账号绑定处理
			//$this->gourl("/index.php?m=register&a=OpenReg");
			$redata=array(
				"authcode"=>$authcode
			);
			$this->goall('注册登陆成功',0,$redata,'/index.php');
		}
	}

}
?>